---
--- 接口限流脚本
--- Created by ZhuHJay.
--- DateTime: 2023/3/20 10:25
---
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local expire = tonumber(ARGV[2])
-- 获取 key 中对应的值
local cacheCount = tonumber(redis.call('GET', key) or  "0")
if (cacheCount == 0) then
    -- 设置值, 并设置过期时间
    redis.call('SET', key, 1)
    redis.call('EXPIRE', key, expire)
    return 1
elseif (cacheCount < limit) then
    -- 自增, 还未超过限制
    redis.call('INCRBY', key, 1)
    return cacheCount + 1
else
    -- 当超过了限制则返回 0
    return 0
end